import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';

const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        name: 'Home',
        component: () => import('@/views/RequestDemo.vue'),
        meta: {
            title: '首页',
            requiresAuth: false,
        },
    },
    // {
    //     path: '/:pathMatch(.*)*',
    //     name: 'NotFound',
    //     component: () => import('@/views/NotFound.vue'),
    //     meta: {
    //         title: '404',
    //         requiresAuth: false,
    //     },
    // },
];

const router = createRouter({
    history: createWebHistory(),
    routes,
});

// 全局前置守卫
router.beforeEach((to, from, next) => {
    // 设置页面标题
    document.title = `${to.meta.title} - ${import.meta.env.VITE_APP_TITLE}`;

    // 判断是否需要登录权限
    if (to.meta.requiresAuth) {
        const token = localStorage.getItem('token');
        if (!token) {
            next({ name: 'Login', query: { redirect: to.fullPath } });
            return;
        }
    }
    next();
});

export default router;
